import path from 'node:path';
import process from 'node:process';

import {
  AutoImportComponents,
  AutoImportPlugin,
  defineConfig,
  viteSvgIconsPlugin,
} from '@vben/vite-config';

import ElementPlus from 'unplugin-element-plus/vite';

export default defineConfig(async () => {
  const root = process.cwd();
  const iconDirPath = path.resolve(root, '../../packages/icons/src/svg/icons');
  const typesPath = path.resolve(root, './src/types');

  return {
    application: {},
    vite: {
      plugins: [
        viteSvgIconsPlugin({
          iconDirs: [iconDirPath],
          symbolId: 'icon-[name]',
        }),
        ElementPlus({
          format: 'esm',
        }),
        AutoImportComponents(
          {
            // 指定组件位置，默认是 src/components 自动导入自定义组件
            dirs: ['./src/components'],
            // 配置文件生成位置
            dts: `${typesPath}/components.d.ts`,
            extensions: ['vue', 'tsx'],
          },
          'element-plus',
        ),
        AutoImportPlugin({
          dts: `${typesPath}/auto-imports.d.ts`,
          // 自动导入 vue 相关函数
          imports: [
            'vue',
            'vue-router',
            {
              // vue 3.5.x
              vue: ['useTemplateRef', 'onWatcherCleanup', 'useId'],
            },
          ],
        }),
      ],
      server: {
        proxy: {
          '/api': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, ''),
            // mock代理目标地址
            target: 'http://api.ruanruyi.cn',
            ws: true,
          },
        },
      },
    },
  };
});
